<?php
/**
 * 个人中心-杂
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/27
 * Time: 上午10:14
 * Updatetime:
 * updater：
 */
namespace App\Controller;


use function GuzzleHttp\Psr7\uri_for;
use lyf\Tree;
use \GatewayWorker\Lib\Gateway;

class CenterController extends BaseController{

    /**
     * @desc 消息类型：1 视频，2 音频，3 报告，4 图片，5 快讯，6 数据，7 会议，8 专栏，9 问答，10 问卷
     * @param $k
     */
    private function messageTypeText($k)
    {
        $list[0] = '全部';
        $list[1] = '报警信息';
        $list[2] = '系统消息';
        return $list[$k];
    }

    /**
     * @desc 消息列表
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function msgList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);

        $uid = $this->userInfo->id;
        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('Message');
        $map['uid'] = $uid;
        if ($param['type']==1){
            $map['type'] = 2;
        }else{
            $map['type'] = 1;
        }

        list($listRet, $page) = _listPage($model, $map, $p, 'is_read asc , create_time desc', 'id,title,content,is_read,type,create_time', $listRows);
        if (!empty($listRet)){
            foreach ($listRet as &$v){
                $v['typeText'] = $this->messageTypeText($v['type']);
            }
        }
        return showList($listRet, $page);
    }

    /**
     * @desc 全部设为已读
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @return array
     */
    public function setMsgAllRead()
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        unset($check);

        $uid = $this->userInfo->id;
        if (empty($uid)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);
        M('message')->where(array('uid'=>$uid,'is_read'=>0))->setField('read_time',NOW_TIME);
        M('message')->where(array('uid'=>$uid,'is_read'=>0))->setField('is_read',1);
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,new \stdClass());
    }

    /**
     * @desc 设置消息为已读
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function setMsgRead($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];

        $operation = M('message')->where(array('id'=>array('in',$id)))->setField('read_time',NOW_TIME);
        $operation = M('message')->where(array('id'=>array('in',$id)))->setField('is_read',1);

        return showErrorMsg(L('_OPERATION_SUCCESS_'));
    }


    /**
     * @desc 消息详情
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function msgDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];
        $msgDetail = M('message')->field('id,title,content,create_time')->find($id);
        if (empty($msgDetail)) return showErrorMsg(L('_DATA_NOT_EXIT_'),100002);
        if (empty($msgDetail['is_read'])){
            M('message')->where(array('id'=>$msgDetail['id']))->setField('read_time',NOW_TIME);
            M('message')->where(array('id'=>$msgDetail['id']))->setField('is_read',1);
        }
        $msgDetail['create_time'] = date('Y-m-d H:i',$msgDetail['create_time']);
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$msgDetail);
    }

    /**
     * @desc 删除消息
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function delMsg($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];
        $operation = M('message')->where(array('id'=>array('in',$id)))->delete();
        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L('_OPERATION_FAIL_'),100002);
    }

    /**
     * @desc 删除所有消息，by the type
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function delAllMsg($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);

        $operation = M('message')->where(['type'=>$param['type'],'uid'=>$this->userInfo->id])->delete();
        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L('_OPERATION_FAIL_'),100002);
    }

    /**
     * @desc 围栏列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('enclosure');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,title,shape,lng,lat,radius,location_id,create_time',$listRows);

        return showList($list,$page);
    }

    /**
     * @desc 围栏详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('enclosure');

        $info = M("enclosure")->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);

        $info['content'] = !empty($info['content']) ? unserialize($info['content']) : [];

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    /**
     * @desc 添加围栏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title,shape');

        unset($check);

        $model = M('enclosure');

        if (!empty($param['title'])) $insert_data['title'] = $param['title'];
        if (!empty($param['lat'])) $insert_data['lat'] = $param['lat'];
        if (!empty($param['lng'])) $insert_data['lng'] = $param['lng'];
        if (!empty($param['radius'])) $insert_data['radius'] = $param['radius']*1000;
        if (!empty($param['shape'])) $insert_data['shape'] = $param['shape'];
        if (!empty($param['location'])) $insert_data['location_id'] = $param['location'];
        if (!empty($param['content'])) $insert_data['content'] = serialize($param['content']);

        $insert_data['create_time'] = time();
        $insert_data['uid'] = $this->userInfo->id;


        $operation = $model->add($insert_data);

        if ($operation) {
            if (!empty($param['car_id'])){

				$enclosure_bind = M('car_enclosure_bind');

				$number = $enclosure_bind->where(['car_id'=>$param['car_id']])->count();

				$number = empty($number)?0:$number;

				$number += 1;

                $insert_data_car    =   [
                    'car_id'    =>  $param['car_id'],
                    'enclosure_id'  =>  $operation,
                    'uid'       =>  $this->userInfo->id,
                'create_time'   =>  time(),
					'number'	=>	$number
                ];

				$enclosure_bind->add($insert_data_car);


				$imme = M('car')->where(['id'=>$param['car_id']])->getField('equipment_no');
				//围栏报警

				$mer_obj = new MerTrackController();

				if ($param['shape']==1){//半径画圆

					$order = 'B05,'.$number.','.$param['lat'].','.$param['lng'].','.$param['radius'].',1,1*';
//					$order = 'B05,'.$number.','.$param['lat'].','.$param['lng'].','.$param['radius'].','.$t_in.','.$t_out.'*';

                    $client_id=M('ws')->where(['imei'=>$imme])->order("id desc")->getField("client_id");
                    $message_content = msg_content($order, $imme);
                    Gateway::sendToClient($client_id, $message_content);

//					$mer_obj->sendMsg($order,$imme);

				}elseif ($param['shape']==3){//多边形


					$str = '';

					$enclosureInfo_content_arr = unserialize($insert_data['content']);

					foreach ($enclosureInfo_content_arr as $db_k=>$db_v){

						$str .= $db_v[0].','.$db_v[1].',';

					}

					$order = 'B11,'.$number.','.$str.',1,1*';
//					$order = 'B11,'.$number.','.$str.','.$t_in.','.$t_out.'*';


                    $client_id=M('ws')->where(['imei'=>$imme])->order("id desc")->getField("client_id");
                    $message_content = msg_content($order, $imme);
                    Gateway::sendToClient($client_id, $message_content);

//					$mer_obj->sendMsg($order,$imme);
				}

			}

            return showErrorMsg(L('_OPERATION_SUCCESS_'));
        }
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车下面的围栏列表
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function enclosureListByCar($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_no');

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car_enclosure_bind');

        $map['uid'] = $this->userInfo->id;

        $car_id = M('car')->where(['car_no'=>$param['car_no']])->getField('id');

		$map['car_id'] = $car_id;

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,enclosure_id,create_time',$listRows);

        if (!empty($list)){
            foreach ($list as $k=>$v){
                $info = M('enclosure')->where(['id'=>$v['enclosure_id']])->find();

                $list[$k]['title'] = $info['title'];
                $list[$k]['shape'] = $info['shape'];
                $list[$k]['lng'] = $info['lng'];
                $list[$k]['lat'] = $info['lat'];
                $list[$k]['radius'] = $info['radius'];
                $list[$k]['location_id'] = $info['location_id'];

                $list[$k]['content'] = !empty($info['content'])?unserialize($info['content']):[];
            }
        }
        return showList($list,$page);
    }

    /**
     * @desc 解除绑定
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function enclosureDelByCar($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car_enclosure_bind');

        $car_id = $model->where(['enclosure_id'=>$param['id']])->getField('car_id');
        $number = $model->where(['enclosure_id'=>$param['id']])->getField('number');

        $equipment_no = M('car')->where(['id'=>$car_id])->getField('equipment_no');

        $client_id=M('ws')->where(['imei'=>$equipment_no])->order("id desc")->getField("client_id");

        $operation = $model->delete($param['id']);

        if ($operation){

//        	$mer_obj = new MerTrackController();

        	$order = 'B06,'.$number.'*';

            $message_content = msg_content($order, $equipment_no);
        	Gateway::sendToClient($client_id, $message_content);
//        	$data=$mer_obj->sendMsg($order,$equipment_no);


		}
        return showErrorMsg(L("_OPERATION_SUCCESS"));

    }

    /**
     * @desc 删除围栏
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosureDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('enclosure');

        $info = M("enclosure")->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);

        $operation = M("enclosure")->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->delete();

        if ($operation) {

        	$bindList = M('car_enclosure_bind')->where(['enclosure_id'=>$param['id']])->select();

			$mer_obj = new MerTrackController();

        	foreach ($bindList as $k=>$v){

				$equipment_no = M('car')->where(['id'=>$v['car_id']])->getField('equipment_no');

				$order = 'B06,'.$v['number'].'*';

				$mer_obj->sendMsg($order,$equipment_no);

				M('car_enclosure_bind')->delete($v['id']);

			}
			return showErrorMsg(L("_OPERATION_SUCCESS_"));
		};
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 围栏下面的地址列表
     * @param $param
     * @author Egor<http://gitee.com/seosite>
     */
    public function addressList($param)
    {
        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('pid');

        unset($check);

        $model = M('loclist');


        if (!empty($param['pid'])){
            $map['parent_id'] = $param['pid'];
        }else{
            $map['deep'] = 1;
        }

        list($list,$page) = _listPage($model,$map,1,null,'id,name,next_city_count','500');

        return showList($list,$page);
    }

    /**
     * @desc 汽车列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car');

        if (!empty($param['keyword'])){
//            $condition['title'] = array('like','%'.$param['keyword'].'%');
            $condition['car_no'] = array('like','%'.$param['keyword'].'%');
            $condition['equipment_no'] = array('like','%'.$param['keyword'].'%');
            $condition['container_no'] = array('like','%'.$param['keyword'].'%');
            $condition['sim_no'] = array('like','%'.$param['keyword'].'%');
            $condition['_logic'] ='or';
            $map['_complex'] = $condition;
        }
        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,car_no,headsmall,equipment_no,sim_no,last_call_time',$listRows);

        return showList($list,$page);
    }

    /**
     * @desc 删除汽车
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('ids');

        unset($check);

        $ids = $param['ids'];

        $model = M('car');

        foreach (explode(',',$ids) as $k=>$v){

            $carInfo = $model->where(['id'=>$v,'uid'=>$this->userInfo->id])->find();
            if (empty($carInfo)) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);
        }


        $map['id'] = array('in',$param['ids']);

        $operation = $model->where($map)->delete();

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 车辆状态：1 正常，2 停用，3 运营，4 停运
     * @author Egor<http://gitee.com/seosite>
     * @param $k
     * @return mixed
     */
    private function carStatusText($k)
    {
        $list[1] = L('CAR_STATUS_1');
        $list[2] = L('CAR_STATUS_2');
        $list[3] = L('CAR_STATUS_3');
        $list[4] = L('CAR_STATUS_4');
        return empty($k)?$list[1]:$list[$k];
    }


    /**
     * @desc 车辆状态列表
     * @author Egor<http://gitee.com/seosite>
     * @return mixed
     */
    private function carStatusList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_STATUS_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_STATUS_2')];
        $list[] = ['id'=>'3','text'=>L('CAR_STATUS_3')];
        $list[] = ['id'=>'4','text'=>L('CAR_STATUS_4')];
        return $list;
    }

    /**
     * @desc logo list
     * @author Egor<http://gitee.com/seosite>
     * @return mixed
     */
    private function carLogoList()
    {
        $list = M('car_logo')->select();
        return $list;
    }

    /**
     * @desc color text
     * @author Egor<http://gitee.com/seosite>
     * @param $k
     * @return mixed
     */
    private function carColorText($k)
    {
        $list[1] = L('CAR_COLOR_1');
        $list[2] = L('CAR_COLOR_2');
        $list[3] = L('CAR_COLOR_3');
        $list[4] = L('CAR_COLOR_4');
        $list[5] = L('CAR_COLOR_5');
        $list[6] = L('CAR_COLOR_6');
        $list[7] = L('CAR_COLOR_7');
        $list[8] = L('CAR_COLOR_8');
        $list[9] = L('CAR_COLOR_9');
        $list[10] = L('CAR_COLOR_10');
        return empty($k)?$list[10]:$list[$k];
    }

    /**
     * @desc color list
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    private function carColorList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_COLOR_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_COLOR_2')];
        $list[] = ['id'=>'3','text'=>L('CAR_COLOR_3')];
        $list[] = ['id'=>'4','text'=>L('CAR_COLOR_4')];
        $list[] = ['id'=>'5','text'=>L('CAR_COLOR_5')];
        $list[] = ['id'=>'6','text'=>L('CAR_COLOR_6')];
        $list[] = ['id'=>'7','text'=>L('CAR_COLOR_7')];
        $list[] = ['id'=>'8','text'=>L('CAR_COLOR_8')];
        $list[] = ['id'=>'9','text'=>L('CAR_COLOR_9')];
        $list[] = ['id'=>'10','text'=>L('CAR_COLOR_10')];
        return $list;
    }


    /**
     * @desc cat personal login status 1 allow ,2 deny
     * @author Egor<http://gitee.com/seosite>
     * @param $k
     * @return mixed
     */
    private function carPersonLoginText($k)
    {
        $list[1] = L('CAR_PERSONLOGIN_1');
        $list[2] = L('CAR_PERSONLOGIN_2');
        return empty($k)?$list[1]:$list[$k];
    }

    private function carPersonLoginList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_PERSONLOGIN_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_PERSONLOGIN_2')];
        return $list;
    }


    private function carContainerTypeText($k)
    {
        $list[1] = L('CAR_CONTAINER_TYPE_1');
        $list[2] = L('CAR_CONTAINER_TYPE_2');
        return empty($k)?$list[1]:$list[$k];
    }

    private function carContainerTypeList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_CONTAINER_TYPE_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_CONTAINER_TYPE_2')];
        return $list;
    }


    private function carBackDoorTypeText($k)
    {
        $list[1] = L('CAR_BACK_DOOR_TYPE_1');
        $list[2] = L('CAR_BACK_DOOR_TYPE_2');
        return empty($k)?$list[1]:$list[$k];
    }

    private function carBackDoorTypeList()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_BACK_DOOR_TYPE_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_BACK_DOOR_TYPE_2')];
        return $list;
    }

    /**
     * @desc 设备数据
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function carEquipmentInfo($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_id');

        unset($check);

        if(!empty($param['id'])){
            $info = M('car_equipment')->find($param['id']);
        }else{
            $info = M('car_equipment')->where(['uid'=>$this->userInfo->id,'car_id'=>$param['car_id']])->find();
        }

        $info = empty($info)?'':$info;

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    public function carEquipmentInfoSave($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_id,id');

        unset($check);

        $model = M('car_equipment');

        $info = $model->find($param['id']);

        if(!empty($info)){
            $save_data_equipment = [
                'id'    =>  $param['id'],
                'speed'     =>  empty($param['t_speed'])?'':$param['t_speed'],
                'outside_area'     =>  empty($param['t_outside_area'])?'':$param['t_outside_area'],
                'inside_area'     =>  empty($param['t_inside_area'])?'':$param['t_inside_area'],
                'battery_dismantle'     =>  empty($param['t_battery_dismantle'])?'':$param['t_battery_dismantle'],
                'temperature'     =>  empty($param['t_temperature'])?'':$param['t_temperature'],
                'unlawful_opening'     =>  empty($param['t_unlawful_opening'])?'':$param['t_unlawful_opening'],
                'parking_overtime'     =>  empty($param['t_parking_overtime'])?'':$param['t_parking_overtime'],
                'offline'     =>  empty($param['t_offline'])?'':$param['t_offline'],
                'online'     =>  empty($param['t_online'])?'':$param['t_online'],
                'reversal'     =>  empty($param['t_reversal'])?'':$param['t_reversal'],
                'shock'     =>  empty($param['t_shock'])?'':$param['t_shock'],
                'door'     =>  empty($param['t_door'])?'':$param['t_door'],
                'start'     =>  empty($param['t_start'])?'':$param['t_start'],
                'cold_open'     =>  empty($param['t_cold_open'])?'':$param['t_cold_open'],
                'cold_close'     =>  empty($param['t_cold_close'])?'':$param['t_cold_close'],
                'low_voltage'     =>  empty($param['t_low_voltage'])?'':$param['t_low_voltage'],
                'ignition'     =>  empty($param['t_ignition'])?'':$param['t_ignition'],
                'stop_ignition'     =>  empty($param['t_stop_ignition'])?'':$param['t_stop_ignition'],
                'temperature_sensor'     =>  empty($param['t_temperature_sensor'])?'':$param['t_temperature_sensor'],
                'gate_magnetic'     =>  empty($param['t_gate_magnetic'])?'':$param['t_gate_magnetic'],
                'ignition_line'     =>  empty($param['t_ignition_line'])?'':$param['t_ignition_line'],
                'oil_sensor'     =>  empty($param['t_oil_sensor'])?'':$param['t_oil_sensor'],
                'humidity'     =>  empty($param['t_humidity'])?'':$param['t_humidity'],
                'one_zone_temperature'     =>  empty($param['t_one_zone_temperature'])?'':$param['t_one_zone_temperature'],
                'one_zone_humidity'     =>  empty($param['t_one_zone_humidity'])?'':$param['t_one_zone_humidity'],
                'two_zone_temperature'     =>  empty($param['t_two_zone_temperature'])?'':$param['t_two_zone_temperature'],
                'two_zone_humidity'     =>  empty($param['t_two_zone_humidity'])?'':$param['t_two_zone_humidity'],
                'three_zone_temperature'     =>  empty($param['t_three_zone_temperature'])?'':$param['t_three_zone_temperature'],
                'three_zone_humidity'     =>  empty($param['t_three_zone_humidity'])?'':$param['t_three_zone_humidity'],
                'four_zone_temperature'     =>  empty($param['t_four_zone_temperature'])?'':$param['t_four_zone_temperature'],
                'four_zone_humidity'     =>  empty($param['t_four_zone_humidity'])?'':$param['t_four_zone_humidity'],
                'refrigerating_machine'     =>  empty($param['t_refrigerating_machine'])?'':$param['t_refrigerating_machine'],
                'refrigeration_alarm'     =>  empty($param['t_refrigeration_alarm'])?'':$param['t_refrigeration_alarm'],
                'high_voltage'     =>  empty($param['t_high_voltage'])?'':$param['t_high_voltage'],
                'start_time'     =>  empty($param['t_start_time'])?'':$param['t_start_time'],
                'end_time'     =>  empty($param['t_end_time'])?'':$param['t_end_time'],
                're_time'     =>  empty($param['t_re_time'])?'':$param['t_re_time'],
                'refresh_time'     =>  empty($param['t_refresh_time'])?'':$param['t_refresh_time'],
            ];

            $model->save($save_data_equipment);

        }else{
            $insert_data_equipment = [
                'car_id'    =>  $param['car_id'],
                'uid'       =>  $this->userInfo->id,
                'speed'     =>  empty($param['t_speed'])?'':$param['t_speed'],
                'outside_area'     =>  empty($param['t_outside_area'])?'':$param['t_outside_area'],
                'inside_area'     =>  empty($param['t_inside_area'])?'':$param['t_inside_area'],
                'battery_dismantle'     =>  empty($param['t_battery_dismantle'])?'':$param['t_battery_dismantle'],
                'temperature'     =>  empty($param['t_temperature'])?'':$param['t_temperature'],
                'unlawful_opening'     =>  empty($param['t_unlawful_opening'])?'':$param['t_unlawful_opening'],
                'parking_overtime'     =>  empty($param['t_parking_overtime'])?'':$param['t_parking_overtime'],
                'offline'     =>  empty($param['t_offline'])?'':$param['t_offline'],
                'online'     =>  empty($param['t_online'])?'':$param['t_online'],
                'reversal'     =>  empty($param['t_reversal'])?'':$param['t_reversal'],
                'shock'     =>  empty($param['t_shock'])?'':$param['t_shock'],
                'low_voltage'     =>  empty($param['t_low_voltage'])?'':$param['t_low_voltage'],
                'ignition'     =>  empty($param['t_ignition'])?'':$param['t_ignition'],
                'stop_ignition'     =>  empty($param['t_stop_ignition'])?'':$param['t_stop_ignition'],
                'temperature_sensor'     =>  empty($param['t_temperature_sensor'])?'':$param['t_temperature_sensor'],
                'gate_magnetic'     =>  empty($param['t_gate_magnetic'])?'':$param['t_gate_magnetic'],
                'ignition_line'     =>  empty($param['t_ignition_line'])?'':$param['t_ignition_line'],
                'oil_sensor'     =>  empty($param['t_oil_sensor'])?'':$param['t_oil_sensor'],
                'humidity'     =>  empty($param['t_humidity'])?'':$param['t_humidity'],
                'one_zone_temperature'     =>  empty($param['t_one_zone_temperature'])?'':$param['t_one_zone_temperature'],
                'one_zone_humidity'     =>  empty($param['t_one_zone_humidity'])?'':$param['t_one_zone_humidity'],
                'two_zone_temperature'     =>  empty($param['t_two_zone_temperature'])?'':$param['t_two_zone_temperature'],
                'two_zone_humidity'     =>  empty($param['t_two_zone_humidity'])?'':$param['t_two_zone_humidity'],
                'three_zone_temperature'     =>  empty($param['t_three_zone_temperature'])?'':$param['t_three_zone_temperature'],
                'three_zone_humidity'     =>  empty($param['t_three_zone_humidity'])?'':$param['t_three_zone_humidity'],
                'four_zone_temperature'     =>  empty($param['t_four_zone_temperature'])?'':$param['t_four_zone_temperature'],
                'four_zone_humidity'     =>  empty($param['t_four_zone_humidity'])?'':$param['t_four_zone_humidity'],
                'refrigerating_machine'     =>  empty($param['t_refrigerating_machine'])?'':$param['t_refrigerating_machine'],
                'refrigeration_alarm'     =>  empty($param['t_refrigeration_alarm'])?'':$param['t_refrigeration_alarm'],
                'high_voltage'     =>  empty($param['t_high_voltage'])?'':$param['t_high_voltage'],
                'start_time'     =>  empty($param['t_start_time'])?'':$param['t_start_time'],
                'end_time'     =>  empty($param['t_end_time'])?'':$param['t_end_time'],
                're_time'     =>  empty($param['t_re_time'])?'':$param['t_re_time'],
                'refresh_time'     =>  empty($param['t_refresh_time'])?'':$param['t_refresh_time'],
                'door'     =>  empty($param['t_door'])?'':$param['t_door'],
                'start'     =>  empty($param['t_start'])?'':$param['t_start'],
                'cold_open'     =>  empty($param['t_cold_open'])?'':$param['t_cold_open'],
                'cold_close'     =>  empty($param['t_cold_close'])?'':$param['t_cold_close'],
            ];

            $model->add($insert_data_equipment);

        }


        return showErrorMsg(L("_OPERATION_SUCCESS_"));
    }


    /**
     * @desc 汽车详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car');

        $info = $model->where(['id'=>$param['id'],'uid'=>$this->userInfo->id])->find();

        if (empty($info)) return showErrorMsg(L("_OPERATION_FAIL_").':'.L('_DATA_NOT_EXIT_'),100002);

        $info['status_text'] = $this->carStatusText($info['status']);

        $info['status_list'] = $this->carStatusList();

        $car_logo_url = M('car_logo')->where(['id'=>$info['logo']])->getField('cover_img');
        $info['logo_url'] = !$car_logo_url?'':$car_logo_url;

        $info['logo_list'] = $this->carLogoList();

        $info['color_status'] = $this->carColorText($info['color']);

        $info['color_list'] = $this->carColorList();
        $info['person_login_text'] = $this->carPersonLoginText($info['person_login']);
        $info['person_login_list'] = $this->carPersonLoginList();

        $info['container_type_text'] = $this->carContainerTypeText($info['container_type']);
        $info['container_type_list'] = $this->carContainerTypeList();


        $info['back_door_type_text'] = $this->carBackDoorTypeText($info['back_door_type']);
        $info['back_door_type_list'] = $this->carBackDoorTypeList();

        //详情内容
        if ($_POST['l']=='en-us'){
            $info['car_type_text'] = M('car_type')->where(['id'=>$info['car_type']])->getField('en_name');
            $info['car_type_list'] = M('car_type')->field('id,en_name text')->select();
        }else{
            $info['car_type_text'] = M('car_type')->where(['id'=>$info['car_type']])->getField('cn_name');
            $info['car_type_list'] = M('car_type')->field('id,cn_name text')->select();
        }

        //设备
        $equipment = M('car_equipment')->where(['car_id'=>$param['id'],'uid'=>$this->userInfo->id])->order('id desc')->find();

        $info['equipment_list'] = $equipment;

        $info['car_equipment_id'] = $equipment['id'];

        //浮动框消息设置
        $car_show = M('car_show')->where(['car_id'=>$info['id']])->find();
        $info['car_float_show'] = $car_show;

        //分组信息

        $info['g_title'] = M('car_group')->where(['id'=>$info['gid']])->getField('title');
        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    /**
     * @desc car status list
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carStatusListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_STATUS_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_STATUS_2')];
        $list[] = ['id'=>'3','text'=>L('CAR_STATUS_3')];
        $list[] = ['id'=>'4','text'=>L('CAR_STATUS_4')];

        return showList($list,new \stdClass());
    }


    /**
     * @desc 车辆型号列表
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carTypeListInterface()
    {
        if ($_POST['l']=='en-us'){
            $car_type_list = M('car_type')->field('id,en_name text')->select();
        }else{
            $car_type_list = M('car_type')->field('id,cn_name text')->select();
        }
        return showList($car_type_list,new \stdClass());
    }


    /**
     * @desc logo list
     * @author Egor<http://gitee.com/seosite>
     * @return mixed
     */
    public function carLogoListInterface()
    {
        $list = M('car_logo')->select();
        return showList($list,new \stdClass());
    }

    /**
     * @desc color list
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carColorListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_COLOR_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_COLOR_2')];
        $list[] = ['id'=>'3','text'=>L('CAR_COLOR_3')];
        $list[] = ['id'=>'4','text'=>L('CAR_COLOR_4')];
        $list[] = ['id'=>'5','text'=>L('CAR_COLOR_5')];
        $list[] = ['id'=>'6','text'=>L('CAR_COLOR_6')];
        $list[] = ['id'=>'7','text'=>L('CAR_COLOR_7')];
        $list[] = ['id'=>'8','text'=>L('CAR_COLOR_8')];
        $list[] = ['id'=>'9','text'=>L('CAR_COLOR_9')];
        $list[] = ['id'=>'10','text'=>L('CAR_COLOR_10')];
        return showList($list,new \stdClass());
    }

    /**
     * @desc 登录列表
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carPersonLoginListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_PERSONLOGIN_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_PERSONLOGIN_2')];
        return showList($list,new \stdClass());
    }


    /**
     * @desc 汽车货柜类型
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carContainerTypeListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_CONTAINER_TYPE_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_CONTAINER_TYPE_2')];
        return showList($list,new \stdClass());
    }


    /**
     * @desc 后门类型
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function carBackDoorTypeListInterface()
    {
        $list[] = ['id'=>'1','text'=>L('CAR_BACK_DOOR_TYPE_1')];
        $list[] = ['id'=>'2','text'=>L('CAR_BACK_DOOR_TYPE_2')];
        return showList($list,new \stdClass());
    }


    /**
     * @desc 汽车操作
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carOperation($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        unset($check);

        $model = M('car');
        $model_show = M('car_show');
        $model_attr = M('car_attr');
        $model_car_equipment = M('car_equipment');



        if (!$param['id']){//添加汽车

            $insert_data['uid'] = $this->userInfo->id;

            //基本信息

            if (empty($param['car_no'])) return showErrorMsg('车牌不能为空',100002);
            $is_exist_carno = M('car')->where(['car_no'=>$param['car_no']])->find();

            if ($is_exist_carno) return showErrorMsg('汽车车牌已存在，请联系客服',100002);

            if (!empty($param['car_no'])) $insert_data['car_no'] = $param['car_no'];
            if (!empty($param['sim_no'])) $insert_data['sim_no'] = $param['sim_no'];
            if (!empty($param['equipment_no'])) $insert_data['equipment_no'] = $param['equipment_no'];
            if (!empty($param['car_model'])) $insert_data['car_model'] = $param['car_model'];
            if (!empty($param['factor'])) $insert_data['factor'] = $param['factor'];
            if (!empty($param['status'])) $insert_data['status'] = $param['status'];
            if (!empty($param['company_id'])) $insert_data['company_id'] = $param['company_id'];
            if (!empty($param['logo'])) $insert_data['logo'] = $param['logo'];
            if (!empty($param['color'])) $insert_data['color'] = $param['color'];
            if (!empty($param['time_zone'])) $insert_data['time_zone'] = $param['time_zone'];
            if (!empty($param['personal_login'])) $insert_data['personal_login'] = $param['personal_login'];
            if (!empty($param['callback_sec'])) $insert_data['callback_sec'] = $param['callback_sec'];
            if (!empty($param['cj_pwd'])) $insert_data['cj_pwd'] = $param['cj_pwd'];
            if (!empty($param['oil_volume'])) $insert_data['oil_volume'] = $param['oil_volume'];
            if (!empty($param['refrigeration_no'])) $insert_data['refrigeration_no'] = $param['refrigeration_no'];
            if (!empty($param['refrigeration_serial_no'])) $insert_data['refrigeration_serial_no'] = $param['refrigeration_serial_no'];
            if (!empty($param['container_no'])) $insert_data['container_no'] = $param['container_no'];
            if (!empty($param['container_via_no'])) $insert_data['container_via_no'] = $param['container_via_no'];
            if (!empty($param['container_type'])) $insert_data['container_type'] = $param['container_type'];
            if (!empty($param['back_door_type'])) $insert_data['back_door_type'] = $param['back_door_type'];
            if (!empty($param['install_time'])) $insert_data['install_time'] = $param['install_time'];
            if (!empty($param['admin_info'])) $insert_data['admin_info'] = $param['admin_info'];
            if (!empty($param['remarks'])) $insert_data['remarks'] = $param['remarks'];
            if (!empty($param['admin_info'])) $insert_data['admin_info'] = $param['admin_info'];
            if (!empty($param['factory_no'])) $insert_data['factory_no'] = $param['factory_no'];
            if (!empty($param['car_type'])) $insert_data['car_type'] = $param['car_type'];
            if (!empty($param['operation_license_no'])) $insert_data['operation_license_no'] = $param['operation_license_no'];
            if (!empty($param['engine_no'])) $insert_data['engine_no'] = $param['engine_no'];
            if (!empty($param['chassis_no'])) $insert_data['chassis_no'] = $param['chassis_no'];
            if (!empty($param['length'])) $insert_data['length'] = $param['length'];
            if (!empty($param['volume'])) $insert_data['volume'] = $param['volume'];
            if (!empty($param['route_site'])) $insert_data['route_site'] = $param['route_site'];
            if (!empty($param['frame_no'])) $insert_data['frame_no'] = $param['frame_no'];
            if (!empty($param['headsmall'])) $insert_data['headsmall'] = $param['headsmall'];
            if (!empty($param['equipment_ids'])) $insert_data['equipment_ids'] = $param['equipment_ids'];
            if (!empty($param['gid'])) $insert_data['gid'] = $param['gid'];
            if (!empty($param['th_head_number_1'])) $insert_data['th_head_number_1'] = $param['th_head_number_1'];
            if (!empty($param['th_head_number_2'])) $insert_data['th_head_number_2'] = $param['th_head_number_2'];
            if (!empty($param['th_head_number_3'])) $insert_data['th_head_number_3'] = $param['th_head_number_3'];
            if (!empty($param['th_head_number_4'])) $insert_data['th_head_number_4'] = $param['th_head_number_4'];


            $operation = $model->add($insert_data);

            if ($operation) {

                //浮动框消息设置
                $insert_data_s = [
                    'car_id'                =>  $operation,
                    'uid'                   =>  $this->userInfo->id,
                    'equipment_title'       =>  empty($param['s_equipment_title'])?0:$param['s_equipment_title'],
                    'gps_time'              =>  empty($param['s_gps_time'])?0:$param['s_gps_time'],
                    'speed'                 =>  empty($param['s_speed'])?0:$param['s_speed'],
                    'status'                =>  empty($param['s_status'])?0:$param['s_status'],
                    'location'              =>  empty($param['s_location'])?0:$param['s_location'],
                    'temperature'           =>  empty($param['s_temperature'])?0:$param['s_temperature'],
                    'humidity'              =>  empty($param['s_humidity'])?0:$param['s_humidity'],
                    'entrance_guard'        =>  empty($param['s_entrance_guard'])?0:$param['s_entrance_guard'],
                    'refrigerator'          =>  empty($param['s_refrigerator'])?0:$param['s_refrigerator'],
                    'oil'                   =>  empty($param['s_oil'])?0:$param['s_oil'],
                    'mileage_now'           =>  empty($param['s_mileage_now'])?0:$param['s_mileage_now'],
                    'mileage_all'           =>  empty($param['s_mileage_all'])?0:$param['s_mileage_all'],
                    'voltage'           =>  empty($param['s_voltage'])?0:$param['s_voltage'],
                    'electric_quantity'           =>  empty($param['s_electric_quantity'])?0:$param['s_electric_quantity'],
                ];
                $operation_s = $model_show->add($insert_data_s);

                //汽车属性，当设备号输入正确后就可以和后端进行通信了

//                $insert_data_a = [
//                    'car_id'    =>  $operation,
//                    'uid'       =>  $this->userInfo->id,
//                ];
//                $operation_a = $model_attr->add($insert_data_a);

                //汽车设备数据添加
//                if (!empty($param['t_w'])) $insert_data_equipment['temperature'] = $param['temperature'];
//                if (!empty($param['t_s'])) $insert_data_equipment['humidity'] = $param['t_s'];
//                if (!empty($param['t_w_1'])) $insert_data_equipment['one_zone_temperature'] = $param['t_w_1'];
//                if (!empty($param['t_s_1'])) $insert_data_equipment['one_zone_humidity'] = $param['t_s_1'];
//                if (!empty($param['t_w_2'])) $insert_data_equipment['two_zone_temperature'] = $param['t_w_2'];
//                if (!empty($param['t_s_2'])) $insert_data_equipment['two_zone_humidity'] = $param['t_s_2'];
//                if (!empty($param['t_w_3'])) $insert_data_equipment['three_zone_temperature'] = $param['t_w_3'];
//                if (!empty($param['t_s_3'])) $insert_data_equipment['three_zone_humidity'] = $param['t_s_3'];
//                if (!empty($param['t_w_4'])) $insert_data_equipment['four_zone_temperature'] = $param['t_w_4'];
//                if (!empty($param['t_s_4'])) $insert_data_equipment['four_zone_humidity'] = $param['t_s_4'];
//                if (!empty($param['t_offline'])) $insert_data_equipment['offline'] = $param['t_offline'];
//                if (!empty($param['t_refrigerating_machine'])) $insert_data_equipment['refrigerating_machine'] = $param['t_refrigerating_machine'];
//                if (!empty($param['t_refrigeration_alarm'])) $insert_data_equipment['refrigeration_alarm'] = $param['t_refrigeration_alarm'];
//                if (!empty($param['t_outside_area'])) $insert_data_equipment['outside_area'] = $param['t_outside_area'];
//                if (!empty($param['t_inside_area'])) $insert_data_equipment['inside_area'] = $param['t_inside_area'];
//                if (!empty($param['t_speed'])) $insert_data_equipment['speed'] = $param['t_speed'];
//                if (!empty($param['t_low_voltage'])) $insert_data_equipment['low_voltage'] = $param['t_low_voltage'];
//                if (!empty($param['t_high_voltage'])) $insert_data_equipment['high_voltage'] = $param['t_high_voltage'];
//                if (!empty($param['t_oil_sensor'])) $insert_data_equipment['oil_sensor'] = $param['t_oil_sensor'];
//                if (!empty($param['t_gate_magnetic'])) $insert_data_equipment['gate_magnetic'] = $param['t_gate_magnetic'];
//                if (!empty($param['t_ignition'])) $insert_data_equipment['ignition'] = $param['t_ignition'];



                $insert_data_equipment = [
                    'car_id'    =>  $operation,
                    'uid'       =>  $this->userInfo->id,
                    'speed'     =>  empty($param['t_speed'])?'':$param['t_speed'],
                    'outside_area'     =>  empty($param['t_outside_area'])?'':$param['t_outside_area'],
                    'inside_area'     =>  empty($param['t_inside_area'])?'':$param['t_inside_area'],
                    'battery_dismantle'     =>  empty($param['t_battery_dismantle'])?'':$param['t_battery_dismantle'],
                    'temperature'     =>  empty($param['t_temperature'])?'':$param['t_temperature'],
                    'unlawful_opening'     =>  empty($param['t_unlawful_opening'])?'':$param['t_unlawful_opening'],
                    'parking_overtime'     =>  empty($param['t_parking_overtime'])?'':$param['t_parking_overtime'],
                    'offline'     =>  empty($param['t_offline'])?'':$param['t_offline'],
                    'online'     =>  empty($param['t_online'])?'':$param['t_online'],
                    'reversal'     =>  empty($param['t_reversal'])?'':$param['t_reversal'],
                    'shock'     =>  empty($param['t_shock'])?'':$param['t_shock'],
                    'door'     =>  empty($param['t_door'])?'':$param['t_door'],
                    'start'     =>  empty($param['t_start'])?'':$param['t_start'],
                    'cold_open'     =>  empty($param['t_cold_open'])?'':$param['t_cold_open'],
                    'cold_close'     =>  empty($param['t_cold_close'])?'':$param['t_cold_close'],
                    'low_voltage'     =>  empty($param['t_low_voltage'])?'':$param['t_low_voltage'],
                    'ignition'     =>  empty($param['t_ignition'])?'':$param['t_ignition'],
                    'stop_ignition'     =>  empty($param['t_stop_ignition'])?'':$param['t_stop_ignition'],
                    'temperature_sensor'     =>  empty($param['t_temperature_sensor'])?'':$param['t_temperature_sensor'],
                    'gate_magnetic'     =>  empty($param['t_gate_magnetic'])?'':$param['t_gate_magnetic'],
                    'ignition_line'     =>  empty($param['t_ignition_line'])?'':$param['t_ignition_line'],
                    'oil_sensor'     =>  empty($param['t_oil_sensor'])?'':$param['t_oil_sensor'],
                    'humidity'     =>  empty($param['t_humidity'])?'':$param['t_humidity'],
                    'one_zone_temperature'     =>  empty($param['t_one_zone_temperature'])?'':$param['t_one_zone_temperature'],
                    'one_zone_humidity'     =>  empty($param['t_one_zone_humidity'])?'':$param['t_one_zone_humidity'],
                    'two_zone_temperature'     =>  empty($param['t_two_zone_temperature'])?'':$param['t_two_zone_temperature'],
                    'two_zone_humidity'     =>  empty($param['t_two_zone_humidity'])?'':$param['t_two_zone_humidity'],
                    'three_zone_temperature'     =>  empty($param['t_three_zone_temperature'])?'':$param['t_three_zone_temperature'],
                    'three_zone_humidity'     =>  empty($param['t_three_zone_humidity'])?'':$param['t_three_zone_humidity'],
                    'four_zone_temperature'     =>  empty($param['t_four_zone_temperature'])?'':$param['t_four_zone_temperature'],
                    'four_zone_humidity'     =>  empty($param['t_four_zone_humidity'])?'':$param['t_four_zone_humidity'],
                    'refrigerating_machine'     =>  empty($param['t_refrigerating_machine'])?'':$param['t_refrigerating_machine'],
                    'refrigeration_alarm'     =>  empty($param['t_refrigeration_alarm'])?'':$param['t_refrigeration_alarm'],
                    'high_voltage'     =>  empty($param['t_high_voltage'])?'':$param['t_high_voltage'],
                    'start_time'     =>  empty($param['t_start_time'])?'':$param['t_start_time'],
                    'end_time'     =>  empty($param['t_end_time'])?'':$param['t_end_time'],
                    're_time'     =>  empty($param['t_re_time'])?'':$param['t_re_time'],
                    'refresh_time'     =>  empty($param['t_refresh_time'])?'':$param['t_refresh_time'],
                ];

                $insert_data_equipment['uid'] = $this->userInfo->id;
                $insert_data_equipment['car_id'] = $operation;

                $model_car_equipment->add($insert_data_equipment);

                if ($operation_s) {

                	//出围栏报警
//                	if (!empty($param['t_outside_area'])||!empty($param['t_inside_area'])){
                	if (!empty($param['t_outside_area'])){


                		$t_out = empty($param['t_outside_area'])?0:1;
                		$t_in = empty($param['t_inside_area'])?0:1;


						$tArea = explode(',',$param['t_outside_area']);


						$iArea = explode(',',$param['t_inside_area']);

						$model_enclosure_bind = M('car_enclosure_bind');
						$model_enclosure = M('enclosure');

						if (!empty($tArea)){
							foreach ($tArea as $t_k => $t_v){

								$number = $model_enclosure_bind->where(['car_id'=>$operation])->count();

								$number = empty($number)?0:$number;

								$number +=1;

								$insert_t_data = [
									'car_id'	=>	$operation,
									'enclosure_id'	=>	$t_v,
									'uid'		=>	$this->userInfo->id,
									'create_time'	=>	time(),
									'number'	=>	$number,
									'type'	=>	1
								];

								$model_enclosure_bind->add($insert_t_data);

								$enclosureInfo = $model_enclosure->find($t_v);

								$mer_obj = new MerTrackController();

								if ($enclosureInfo['shape']==1){//半径画圆


									//sprintf("%01.3f",'1.01')
									$order = 'B05,'.$number.','.sprintf("%01.6f",$enclosureInfo['lat']).','.sprintf("%01.6f",$enclosureInfo['lng']).','.$enclosureInfo['radius'].',0,1*';

									$mer_obj->sendMsg($order,$insert_data['equipment_no']);

								}elseif ($enclosureInfo['shape']==3){//多边形


									$str = '';

									$enclosureInfo_content_arr = unserialize($enclosureInfo['content']);

									foreach ($enclosureInfo_content_arr as $db_k=>$db_v){

										$str .= sprintf("%01.6f",$db_v[0]).','.sprintf("%01.6f",$db_v[1]).',';

									}

									$order = 'B11,'.$number.','.$str.',0,1*';

									$mer_obj->sendMsg($order,$insert_data['equipment_no']);
								}

							}
						}


						if (!empty($iArea)){
							foreach ($iArea as $t_k => $t_v){

								$number = $model_enclosure_bind->where(['car_id'=>$operation])->count();

								$number = empty($number)?0:$number;

								$number +=1;

								$insert_t_data = [
									'car_id'	=>	$operation,
									'enclosure_id'	=>	$t_v,
									'uid'		=>	$this->userInfo->id,
									'create_time'	=>	time(),
									'number'	=>	$number,
									'type'	=>	2
								];

								$model_enclosure_bind->add($insert_t_data);

								$enclosureInfo = $model_enclosure->find($t_v);

								$mer_obj = new MerTrackController();

								if ($enclosureInfo['shape']==1){//半径画圆

									$order = 'B05,'.$number.','.sprintf("%01.6f",$enclosureInfo['lat']).','.sprintf("%01.6f",$enclosureInfo['lng']).','.$enclosureInfo['radius'].',1,0*';

									$mer_obj->sendMsg($order,$insert_data['equipment_no']);

								}elseif ($enclosureInfo['shape']==3){//多边形


									$str = '';

									$enclosureInfo_content_arr = unserialize($enclosureInfo['content']);

									foreach ($enclosureInfo_content_arr as $db_k=>$db_v){

										$str .= sprintf("%01.6f",$db_v[0]).','.sprintf("%01.6f",$db_v[1]).',';

									}

									$order = 'B11,'.$number.','.$str.',1,0*';

									$mer_obj->sendMsg($order,$insert_data['equipment_no']);
								}

							}
						}



					}

                    return showErrorMsg(L("_OPERATION_SUCCESS_"));
                }else{
                    $model->delete($operation);
                    $model_show->delete($operation_s);
                    return showErrorMsg(L("_OPERATION_FAIL_"),10002);
                }
            }
            return showErrorMsg(L("_OPERATION_FAIL_"),10002);
        }else{//修改
            $save_data['id'] = $param['id'];

            if (!empty($param['car_no'])) $save_data['car_no'] = $param['car_no'];
            if (!empty($param['sim_no'])) $save_data['sim_no'] = $param['sim_no'];
            if (!empty($param['equipment_no'])) $save_data['equipment_no'] = $param['equipment_no'];
            if (!empty($param['car_model'])) $save_data['car_model'] = $param['car_model'];
            if (!empty($param['factor'])) $save_data['factor'] = $param['factor'];
            if (!empty($param['status'])) $save_data['status'] = $param['status'];
            if (!empty($param['company_id'])) $save_data['company_id'] = $param['company_id'];
            if (!empty($param['logo'])) $save_data['logo'] = $param['logo'];
            if (!empty($param['color'])) $save_data['color'] = $param['color'];
            if (!empty($param['time_zone'])) $save_data['time_zone'] = $param['time_zone'];
            if (!empty($param['personal_login'])) $save_data['personal_login'] = $param['personal_login'];
            if (!empty($param['callback_sec'])) $save_data['callback_sec'] = $param['callback_sec'];
            if (!empty($param['cj_pwd'])) $save_data['cj_pwd'] = $param['cj_pwd'];
            if (!empty($param['oil_volume'])) $save_data['oil_volume'] = $param['oil_volume'];
            if (!empty($param['refrigeration_no'])) $save_data['refrigeration_no'] = $param['refrigeration_no'];
            if (!empty($param['refrigeration_serial_no'])) $save_data['refrigeration_serial_no'] = $param['refrigeration_serial_no'];
            if (!empty($param['container_no'])) $save_data['container_no'] = $param['container_no'];
            if (!empty($param['container_via_no'])) $save_data['container_via_no'] = $param['container_via_no'];
            if (!empty($param['container_type'])) $save_data['container_type'] = $param['container_type'];
            if (!empty($param['back_door_type'])) $save_data['back_door_type'] = $param['back_door_type'];
            if (!empty($param['install_time'])) $save_data['install_time'] = $param['install_time'];
            if (!empty($param['admin_info'])) $save_data['admin_info'] = $param['admin_info'];
            if (!empty($param['remarks'])) $save_data['remarks'] = $param['remarks'];
            if (!empty($param['admin_info'])) $save_data['admin_info'] = $param['admin_info'];
            if (!empty($param['factory_no'])) $save_data['factory_no'] = $param['factory_no'];
            if (!empty($param['car_type'])) $save_data['car_type'] = $param['car_type'];
            if (!empty($param['operation_license_no'])) $save_data['operation_license_no'] = $param['operation_license_no'];
            if (!empty($param['engine_no'])) $save_data['engine_no'] = $param['engine_no'];
            if (!empty($param['chassis_no'])) $save_data['chassis_no'] = $param['chassis_no'];
            if (!empty($param['length'])) $save_data['length'] = $param['length'];
            if (!empty($param['volume'])) $save_data['volume'] = $param['volume'];
            if (!empty($param['route_site'])) $save_data['route_site'] = $param['route_site'];
            if (!empty($param['frame_no'])) $save_data['frame_no'] = $param['frame_no'];
            if (!empty($param['headsmall'])) $save_data['headsmall'] = $param['headsmall'];
            if (!empty($param['equipment_ids'])) $save_data['equipment_ids'] = $param['equipment_ids'];
            if (!empty($param['gid'])) $save_data['gid'] = $param['gid'];
            if (!empty($param['th_head_number_1'])) $save_data['th_head_number_1'] = $param['th_head_number_1'];
            if (!empty($param['th_head_number_2'])) $save_data['th_head_number_2'] = $param['th_head_number_2'];
            if (!empty($param['th_head_number_3'])) $save_data['th_head_number_3'] = $param['th_head_number_3'];
            if (!empty($param['th_head_number_4'])) $save_data['th_head_number_4'] = $param['th_head_number_4'];

            $save_data['update_time'] = time();

            $operation = $model->save($save_data);

            if ($operation){

                //修改浮动框消息
                if (!empty($param['car_show_id'])){

                    $save_data_s['id'] = $param['car_show_id'];
                    if (!empty($param['s_equipment_title'])) $save_data_s['equipment_title'] = $param['s_equipment_title'];
                    if (!empty($param['s_gps_time'])) $save_data_s['gps_time'] = $param['s_gps_time'];
                    if (!empty($param['s_speed'])) $save_data_s['speed'] = $param['s_speed'];
                    if (!empty($param['s_status'])) $save_data_s['status'] = $param['s_status'];
                    if (!empty($param['s_location'])) $save_data_s['location'] = $param['s_location'];
                    if (!empty($param['s_temperature'])) $save_data_s['temperature'] = $param['s_temperature'];
                    if (!empty($param['s_humidity'])) $save_data_s['humidity'] = $param['s_humidity'];
                    if (!empty($param['s_entrance_guard'])) $save_data_s['entrance_guard'] = $param['s_entrance_guard'];
                    if (!empty($param['s_oil'])) $save_data_s['oil'] = $param['s_oil'];
                    if (!empty($param['s_mileage_now'])) $save_data_s['mileage_now'] = $param['s_mileage_now'];
                    if (!empty($param['s_mileage_all'])) $save_data_s['mileage_all'] = $param['s_mileage_all'];
                    if (!empty($param['s_voltage'])) $save_data_s['voltage'] = $param['s_voltage'];
                    if (!empty($param['s_mileage_all'])) $save_data_s['mileage_all'] = $param['s_mileage_all'];
                    if (!empty($param['s_electric_quantity'])) $save_data_s['electric_quantity'] = $param['s_electric_quantity'];

                    $operation_s = $model_show->save($save_data_s);

                    if ($operation_s) {
                        return showErrorMsg(L("_OPERATION_SUCCESS_"));
                    }else{
                        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
                    }
                }

                //修改汽车设备
                if (!empty($param['car_equipment_id'])){



                    $save_data_equipment = [
                        'id'    =>  $param['car_equipment_id'],
                        'speed'     =>  empty($param['t_speed'])?'':$param['t_speed'],
                        'outside_area'     =>  empty($param['t_outside_area'])?'':$param['t_outside_area'],
                        'inside_area'     =>  empty($param['t_inside_area'])?'':$param['t_inside_area'],
                        'battery_dismantle'     =>  empty($param['t_battery_dismantle'])?'':$param['t_battery_dismantle'],
                        'temperature'     =>  empty($param['t_temperature'])?'':$param['t_temperature'],
                        'unlawful_opening'     =>  empty($param['t_unlawful_opening'])?'':$param['t_unlawful_opening'],
                        'parking_overtime'     =>  empty($param['t_parking_overtime'])?'':$param['t_parking_overtime'],
                        'offline'     =>  empty($param['t_offline'])?'':$param['t_offline'],
                        'online'     =>  empty($param['t_online'])?'':$param['t_online'],
                        'reversal'     =>  empty($param['t_reversal'])?'':$param['t_reversal'],
                        'shock'     =>  empty($param['t_shock'])?'':$param['t_shock'],
                        'door'     =>  empty($param['t_door'])?'':$param['t_door'],
                        'cold_open'     =>  empty($param['t_cold_open'])?'':$param['t_cold_open'],
                        'cold_close'     =>  empty($param['t_cold_close'])?'':$param['t_cold_close'],
                        'start'     =>  empty($param['t_start'])?'':$param['t_start'],
                        'low_voltage'     =>  empty($param['t_low_voltage'])?'':$param['t_low_voltage'],
                        'ignition'     =>  empty($param['t_ignition'])?'':$param['t_ignition'],
                        'stop_ignition'     =>  empty($param['t_stop_ignition'])?'':$param['t_stop_ignition'],
                        'temperature_sensor'     =>  empty($param['t_temperature_sensor'])?'':$param['t_temperature_sensor'],
                        'gate_magnetic'     =>  empty($param['t_gate_magnetic'])?'':$param['t_gate_magnetic'],
                        'ignition_line'     =>  empty($param['t_ignition_line'])?'':$param['t_ignition_line'],
                        'oil_sensor'     =>  empty($param['t_oil_sensor'])?'':$param['t_oil_sensor'],
                        'humidity'     =>  empty($param['t_humidity'])?'':$param['t_humidity'],
                        'one_zone_temperature'     =>  empty($param['t_one_zone_temperature'])?'':$param['t_one_zone_temperature'],
                        'one_zone_humidity'     =>  empty($param['t_one_zone_humidity'])?'':$param['t_one_zone_humidity'],
                        'two_zone_temperature'     =>  empty($param['t_two_zone_temperature'])?'':$param['t_two_zone_temperature'],
                        'two_zone_humidity'     =>  empty($param['t_two_zone_humidity'])?'':$param['t_two_zone_humidity'],
                        'three_zone_temperature'     =>  empty($param['t_three_zone_temperature'])?'':$param['t_three_zone_temperature'],
                        'three_zone_humidity'     =>  empty($param['t_three_zone_humidity'])?'':$param['t_three_zone_humidity'],
                        'four_zone_temperature'     =>  empty($param['t_four_zone_temperature'])?'':$param['t_four_zone_temperature'],
                        'four_zone_humidity'     =>  empty($param['t_four_zone_humidity'])?'':$param['t_four_zone_humidity'],
                        'refrigerating_machine'     =>  empty($param['t_refrigerating_machine'])?'':$param['t_refrigerating_machine'],
                        'refrigeration_alarm'     =>  empty($param['t_refrigeration_alarm'])?'':$param['t_refrigeration_alarm'],
                        'high_voltage'     =>  empty($param['t_high_voltage'])?'':$param['t_high_voltage'],
                        'start_time'     =>  empty($param['t_start_time'])?'':$param['t_start_time'],
                        'end_time'     =>  empty($param['t_end_time'])?'':$param['t_end_time'],
                        're_time'     =>  empty($param['t_re_time'])?'':$param['t_re_time'],
                        'refresh_time'     =>  empty($param['t_refresh_time'])?'':$param['t_refresh_time'],
                    ];

                    $model_car_equipment->save($save_data_equipment);



					//围栏报警,先删除，后添加
					$car_id = $param['id'];

					$model_enclosure_bind = M('car_enclosure_bind');

					$mer_obj = new MerTrackController();

					$bind_en_list = $model_enclosure_bind->where(['car_id'=>$car_id])->select();

					$equipment_no = M('car')->where(['id'=>$car_id])->getField('equipment_no');

					if (!empty($bind_en_list)){
                        foreach ($bind_en_list as $k=>$v){

                            $order = 'B06,'.$v['number'].'*';

                            $mer_obj = new MerTrackController();
                            $mer_obj->sendMsg($order,$equipment_no);

                        }
                    }

					$model_enclosure_bind->where(['car_id'=>$car_id])->delete();


					//删除完所有的，重新添加


					if (!empty($param['t_outside_area'])||!empty($param['t_inside_area'])){


						$t_out = empty($param['t_outside_area'])?0:1;
						$t_in = empty($param['t_inside_area'])?0:1;

						$tArea = explode(',',$param['t_outside_area']);
						$tIArea = explode(',',$param['t_inside_area']);

						$model_enclosure_bind = M('car_enclosure_bind');
						$model_enclosure = M('enclosure');


						if (!empty($param['t_outside_area'])){
							foreach ($tArea as $t_k => $t_v){

								$number = $model_enclosure_bind->where(['car_id'=>$car_id])->count();

								$number = empty($number)?0:$number;

								$number +=1;

								$insert_t_data = [
									'car_id'	=>	$car_id,
									'enclosure_id'	=>	$t_v,
									'uid'		=>	$this->userInfo->id,
									'create_time'	=>	time(),
									'number'	=>	$number,
									'type'	=>	1
								];

								$model_enclosure_bind->add($insert_t_data);

								$enclosureInfo = $model_enclosure->find($t_v);



								if ($enclosureInfo['shape']==1){//半径画圆

									$order = 'B05,'.$number.','.sprintf("%01.6f",$enclosureInfo['lat']).','.sprintf("%01.6f",$enclosureInfo['lng']).','.$enclosureInfo['radius'].',0,1*';

									file_put_contents('./out.txt',$order."\n",FILE_APPEND);

									$mer_obj = new MerTrackController();
									$mer_obj->sendMsg($order,$equipment_no);

								}elseif ($enclosureInfo['shape']==3){//多边形


									$str = '';

									$enclosureInfo_content_arr = unserialize($enclosureInfo['content']);

									foreach ($enclosureInfo_content_arr as $db_k=>$db_v){

										$str .= sprintf("%01.6f",$db_v[1]).','.sprintf("%01.6f",$db_v[0]).',';

									}

									$order = 'B11,'.$number.','.$str.',0,1*';

									file_put_contents('./out'.$number.'txt',$order);

									$mer_obj = new MerTrackController();
									$mer_obj->sendMsg($order,$equipment_no);
								}

							}
						}

						if (!empty($param['t_inside_area'])){
							foreach ($tIArea as $t_k2 => $t_v2){

								$number = $model_enclosure_bind->where(['car_id'=>$car_id])->count();

								$number = empty($number)?0:$number;

								$number +=1;

								$insert_t_data = [
									'car_id'	=>	$car_id,
									'enclosure_id'	=>	$t_v2,
									'uid'		=>	$this->userInfo->id,
									'create_time'	=>	time(),
									'number'	=>	$number,
									'type'	=>	2
								];

								$model_enclosure_bind->add($insert_t_data);

								$enclosureInfo = $model_enclosure->find($t_v2);



								if ($enclosureInfo['shape']==1){//半径画圆

									$order = 'B05,'.$number.','.sprintf("%01.6f",$enclosureInfo['lat']).','.sprintf("%01.6f",$enclosureInfo['lng']).','.$enclosureInfo['radius'].',1,0*';

									file_put_contents('./in'.$number.'txt',$order);

									$mer_obj = new MerTrackController();
									$mer_obj->sendMsg($order,$equipment_no);

								}elseif ($enclosureInfo['shape']==3){//多边形


									$str = '';

									$enclosureInfo_content_arr = unserialize($enclosureInfo['content']);

									foreach ($enclosureInfo_content_arr as $db_k2=>$db_v2){

										$str .= sprintf("%01.6f",$db_v2[1]).','.sprintf("%01.6f",$db_v2[0]).',';

									}

									$order = 'B11,'.$number.','.$str.',1,0*';

									file_put_contents('./in'.$number.'txt',$order);

									$mer_obj = new MerTrackController();
									$mer_obj->sendMsg($order,$equipment_no);
								}

							}
						}

					}

				}

                return showErrorMsg(L("_OPERATION_SUCCESS_"));

            }
            return showErrorMsg(L("_OPERATION_FAIL_"),10002);
        }

    }

    /**
     * @desc 汽车分组列表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('car_group');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'num asc,update_time desc,create_time desc','id,title',$listRows);

        $model_car = M('car');
        if (!empty($list)){
            foreach ($list as $k=>$v){
                $car_list = $model_car->field('id,headsmall,car_no,equipment_no,sim_no,last_call_time')->where(['gid'=>$v['id']])->select();
                $list[$k]['_child'] = empty($car_list)?[]:$car_list;
            }
        }
        return showList($list,$page);
    }

    /**
     * @desc 汽车分组详情
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function carGroupDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $info = M('car_group')->find($param['id']);

        if (empty($info)) return showErrorMsg(L('_DATA_EXIST_'),100002);

        $info['company_title'] = M('company')->where(['id'=>$info['company_id']])->getField('title');

        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$info);
    }

    /**
     * @desc 添加群
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title,company_id');

        unset($check);


        $insert_data = [
            'create_time' => time(),
            'update_time' => time(),
            'uid'   =>  $this->userInfo->id,
            'title' =>  $param['title'],
            'num'   =>  empty($param['num'])?50:$param['num'],
            'company_id'    =>  $param['company_id'],
            'remark'    =>  $param['remark']
        ];

        $model = M('car_group');

        $operation = $model->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 删除群
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupDelete($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('car_group');

        $is_car_group_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->find();
        if (!$is_car_group_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        $operation = $model->delete($param['id']);

        if($operation){
            M('car')->where(['gid'=>$param['id']])->setField('gid','0');
            return showErrorMsg(L("_OPERATION_SUCCESS_"));
        }else{
            return showErrorMsg(L("_OPERATION_FAIL_"),10002);
        }

    }

    /**
     * @desc 汽车分组编辑
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function carGroupEdit($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        if (!empty($param['title'])) $save_data['title'] = $param['title'];
        if (!empty($param['num'])) $save_data['num'] = $param['num'];
        if (!empty($param['company_id'])) $save_data['company_id'] = $param['company_id'];
        if (!empty($param['remark'])) $save_data['remark'] = $param['remark'];
        $save_data['update_time'] = time();
        $save_data['id'] = $param['id'];

        $operation = M('car_group')->save($save_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 添加车到群里
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carGroupAddCar($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('group_id,car_id');

        unset($check);

        $model = M('car_group');

        $is_car_group_exit = $model->where(['uid'=>$this->userInfo->id,'id'=>$param['group_id']])->find();
        if (!$is_car_group_exit) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);

        $model_car = M('car');

        $is_car_exist = $model_car->where(['uid'=>$this->userInfo->id,'id'=>$param['car_id']])->find();
        if (!$is_car_exist) return showErrorMsg(L("_OPERATION_FAIL_").': '.L('_DATA_NOT_EXIT_'),100002);


        $operation = $model_car->where(['id'=>$param['car_id']])->setField('gid',$param['group_id']);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 汽车设备详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carEquipmentDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        //设备
        $equipmentAllArr = M('user_equipment')->where(['uid'=>$this->userInfo->id])->select();

        $equipmentArr = [];

        if (!empty($equipmentAllArr)){
            if ($_POST['l'] == 'en-us'){
                foreach ($equipmentAllArr as $k=>$v){

                    $equipment_info = M('equipment')->field('id,en_name text,cn_name,type')->find($v['equipment_id']);

                    if ($equipmentArr[$equipment_info['cn_name']] == '1区温度') $equipmentArr[$equipment_info['cn_name']] = '一区温度';
                    if ($equipmentArr[$equipment_info['cn_name']] == '1区温度') $equipmentArr[$equipment_info['cn_name']] = '一区湿度';
                    if ($equipmentArr[$equipment_info['cn_name']] == '2区温度') $equipmentArr[$equipment_info['cn_name']] = '二区温度';
                    if ($equipmentArr[$equipment_info['cn_name']] == '2区温度') $equipmentArr[$equipment_info['cn_name']] = '二区湿度';
                    if ($equipmentArr[$equipment_info['cn_name']] == '3区温度') $equipmentArr[$equipment_info['cn_name']] = '三区温度';
                    if ($equipmentArr[$equipment_info['cn_name']] == '3区温度') $equipmentArr[$equipment_info['cn_name']] = '三区湿度';
                    if ($equipmentArr[$equipment_info['cn_name']] == '4区温度') $equipmentArr[$equipment_info['cn_name']] = '四区温度';
                    if ($equipmentArr[$equipment_info['cn_name']] == '4区温度') $equipmentArr[$equipment_info['cn_name']] = '四区湿度';

                    $equipmentArr[$equipment_info['cn_name']] = $equipment_info;

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = '2';

                    $equipmentArr[$equipment_info['cn_name']]['value'] = $v['value'];




                    unset($equipmentArr[$equipment_info['cn_name']]['cn_name']);
                }
            }else{
                foreach ($equipmentAllArr as $k=>$v){

                    $equipment_info = M('equipment')->field('id,cn_name text,cn_name,type')->find($v['equipment_id']);

                    $equipmentArr[$equipment_info['cn_name']] = $equipment_info;

                    $equipmentArr[$equipment_info['cn_name']]['is_selected'] = '2';

                    $equipmentArr[$equipment_info['cn_name']]['value'] = $v['value'];

                    unset($equipmentArr[$equipment_info['cn_name']]['cn_name']);
                }
            }
        }

        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$equipmentArr);
    }

    /**
     * @desc 汽车浮动消息设置详情
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function carFloatDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $info = [
            [
                'id'    =>  'equipment_title',
                'text'  =>  L('CAR_FLOAT_MSG_1')
            ],
            [
                'id'    =>  'gps_time',
                'text'  =>  L('CAR_FLOAT_MSG_2')
            ],
            [
                'id'    =>  'speed',
                'text'  =>  L('CAR_FLOAT_MSG_3')
            ],
            [
                'id'    =>  'status',
                'text'  =>  L('CAR_FLOAT_MSG_4')
            ],
            [
                'id'    =>  'location',
                'text'  =>  L('CAR_FLOAT_MSG_5')
            ],
            [
                'id'    =>  'temperature',
                'text'  =>  L('CAR_FLOAT_MSG_6')
            ],
            [
                'id'    =>  'humidity',
                'text'  =>  L('CAR_FLOAT_MSG_7')
            ],
            [
                'id'    =>  'entrance_guard',
                'text'  =>  L('CAR_FLOAT_MSG_8')
            ],
            [
                'id'    =>  'refrigerator',
                'text'  =>  L('CAR_FLOAT_MSG_9')
            ],
            [
                'id'    =>  'oil',
                'text'  =>  L('CAR_FLOAT_MSG_10')
            ],
            [
                'id'    =>  'mileage_now',
                'text'  =>  L('CAR_FLOAT_MSG_11')
            ],
            [
                'id'    =>  'mileage_all',
                'text'  =>  L('CAR_FLOAT_MSG_12')
            ],
            [
                'id'    =>  'voltage',
                'text'  =>  L('CAR_FLOAT_MSG_13')
            ],
            [
                'id'    =>  'electric_quantity',
                'text'  =>  L('CAR_FLOAT_MSG_14')
            ]

        ];

        return showList($info,new \stdClass());
    }


    /**
     * @desc 关于我们
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function aboutUs()
    {
        $return_data = C('SITE');

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$return_data);
    }


    /**
     * @desc 系统设置
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function systemConfig($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $model = M('system_config');
        $info = $model->where(['uid'=>$this->userInfo->id])->find();


        if (empty($info)){
            $info   =  [
                'uid'   =>  $this->userInfo->id,
                'timezone'  =>  empty($param['timezone'])?1:2,
                'speed' =>  empty($param['speed'])?1:2,
                'temperature'   =>  empty($param['temperature'])?1:2,
                'language'  =>  empty($param['language'])?1:2
            ];
            $model->add($info);

        }
        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    /**
     * @desc update system config
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function systemConfigSet($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $model = M('system_config');

        $info = $model->where(['uid'=>$this->userInfo->id])->find();

        if (empty($info)){
            $insert_data = [
                'uid'   =>  $this->userInfo->id,
                'timezone'  =>  $param['timezone'],
                'speed' =>  $param['speed'],
                'temperature'   =>  $param['temperature'],
                'language'  =>  $param['language']
            ];
            $model->add($insert_data);
        }else{
            $save_data = [
                'timezone'  =>  $param['timezone'],
                'speed' =>  $param['speed'],
                'temperature'   =>  $param['temperature'],
                'language'  =>  $param['language']
            ];
            $model->where(['id'=>$info['id']])->save($save_data);
        }

        $info = $model->where(['uid'=>$this->userInfo->id])->find();
        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }


    /**
     * @desc 客户列表
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function companyList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('company');

        $map['uid'] = $this->userInfo->id;

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,title',$listRows);

        return showList($list,$page);
    }


    /**
     * @desc 客户详情
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function companyDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $info = M('company')->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->find();

        if (empty($info)) return showErrorMsg(L("_DATA_EXIST_"),100002);

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$info);
    }

    /**
     * @desc 删除客户
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function companyDel($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $info = M('company')->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->find();

        if (empty($info)) return showErrorMsg(L("_DATA_EXIST_"),100002);

        $operation = M('company')->where(['uid'=>$this->userInfo->id,'id'=>$param['id']])->delete();

        return showErrorMsg(L("_OPERATION_SUCCESS_"));
    }


    /**
     * @desc 添加客户
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function companyAdd($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('title,fr,tel,address');

        unset($check);

        $insert_data = $param;

        $insert_data['uid'] = $this->userInfo->id;
        $insert_data['create_time'] = time();

        $operation = M('company')->add($insert_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }

    /**
     * @desc 客户编辑
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function companyEdit($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isOperationAdmin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        if (!empty($param['title'])) $save_data['title'] = $param['title'];
        if (!empty($param['fr'])) $save_data['fr'] = $param['fr'];
        if (!empty($param['tel'])) $save_data['tel'] = $param['tel'];
        if (!empty($param['address'])) $save_data['address'] = $param['address'];
        $save_data['id'] = $param['id'];

        $operation = M('company')->save($save_data);

        if ($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),10002);
    }


    /**
     * @desc 车机生产厂家
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function carManufacturer()
    {
        $list = M('car_sccj')->select();

        return showList($list, new \stdClass());
    }
}